package cn.tedu.wenzhang1.controller;


import cn.tedu.wenzhang1.base.response.JsonResult;
import cn.tedu.wenzhang1.base.response.StatusCode;
import cn.tedu.wenzhang1.controllerInterface.UserIn;
import cn.tedu.wenzhang1.mapper.UserMapper;
import cn.tedu.wenzhang1.pojo.dto.UserLoginDTO;
import cn.tedu.wenzhang1.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("v1/users/")
@Api(tags = "用户模块")
public class UserController implements UserIn {
    @Autowired(required = false)
    private UserMapper userMapper;


    @PostMapping("login")
    @ApiOperation(value = "登录")
    public JsonResult login(@Validated @RequestBody UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session){
        UserVO userVO=userMapper.selectUser(userLoginDTO.getUsername());
        if (userVO!=null){
            if (userVO.getPassword().equals(userLoginDTO.getPassword())){
                session.setAttribute("user",userVO);
                return JsonResult.ok();
            }
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROE);
        }
        return new JsonResult(StatusCode.USERNAME_ERROE);
    }




}
